package com.xafc.demo.sys.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.CaseFormat;
import com.xafc.demo.sys.dao.DeptMapper;
import com.xafc.demo.sys.model.dto.DeptDto;
import com.xafc.demo.sys.model.entity.Dept;
import com.xafc.demo.framework.result.ResultCode;
import com.xafc.demo.sys.model.query.DeptPageQuery;
import com.xafc.demo.sys.service.IDeptService;
import com.xafc.demo.security.utils.UserHolderUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {

    @Override
    public Page<DeptDto> getDeptList(DeptPageQuery query) {
        // 设置分页
        final Page<DeptDto> page = new Page<>(query.getPage(), query.getLimit());
        // 设置排序
        if(StrUtil.isNotEmpty(query.getOrder())){
            // 驼峰转换
            String field = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, query.getField());
            String order = query.getOrder();
            page.setOrders(
                    ListUtil.toList(
                            StrUtil.equals("asc", order) ? OrderItem.asc(field) : OrderItem.desc(field)
                    ));
        }else {
            // 默认排序
            page.setOrders(ListUtil.toList(OrderItem.desc("id")));
        }

        List<DeptDto> deptDtoList = baseMapper.selectDeptList(page, query);
        page.setRecords(deptDtoList);
        return page;
    }

    @Override
    public Dept getDeptById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<Dept> getDeptByIds(List<Integer> ids) {
//        return baseMapper.selectBatchIds(ids);
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name").in("id", ids);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public ResultCode addDept(Dept dept) {
        ResultCode resultCode;
        Integer count = baseMapper.selectCount(new QueryWrapper<Dept>().eq("name", dept.getName()));
        //部门已存在
        if (count > 0){
            resultCode = ResultCode.fail("部门已存在");
        }else {
            if(dept.getSort() == null)  dept.setSort(0);
            if(dept.getStatus() == null)  dept.setStatus(1);
            dept.setCreateTime(new Date());
            dept.setCreator(UserHolderUtils.getUserId());

            int num= baseMapper.insert(dept);
            //新增成功
            if (num>0){
                resultCode = ResultCode.success("新增成功");
            }else {
                resultCode = ResultCode.error( "新增失败");
            }
        }
        return resultCode;
    }

    @Override
    public ResultCode updateDept(Dept dept) {
        ResultCode resultCode;
        Integer count = baseMapper.selectCount(new QueryWrapper<Dept>().eq("id", dept.getId()));
        if(count > 0){
            dept.setUpdateTime(new Date());
            dept.setUpdater(UserHolderUtils.getUserId());
            int num = baseMapper.updateById(dept);
            if(num>0){
                resultCode = ResultCode.success("更新成功");
            }else {
                resultCode = ResultCode.error("更新失败");
            }
        }else {
            resultCode = ResultCode.fail("无效的部门");
        }
        return resultCode;
    }

    @Override
    public ResultCode deleteDept(Integer id) {
        ResultCode resultCode;
        Integer count = baseMapper.selectCount(new QueryWrapper<Dept>().eq("id", id));
        if(count > 0){
            int num = baseMapper.deleteById(id);
            if(num>0){
                resultCode = ResultCode.success("删除成功");
            }else {
                resultCode = ResultCode.error("删除失败");
            }
        }else {
            resultCode = ResultCode.fail("无效的用户");
        }
        return resultCode;
    }
}
